define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'zhongzhuan/feiyu/huichuan/index/crmids/' + Config.crmids + location.search,
                    add_url: 'zhongzhuan/feiyu/huichuan/add',
                    edit_url: 'zhongzhuan/feiyu/huichuan/edit',
                    del_url: 'zhongzhuan/feiyu/huichuan/del',
                    multi_url: 'zhongzhuan/feiyu/huichuan/multi',
                    import_url: 'zhongzhuan/feiyu/huichuan/import',
                    table: 'ec_aiuser_to_ecuser_feiyu_log',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),searchable:false},
//                        {field: 'aiuser_to_ecuser_feiyu_id', title: __('Aiuser_to_ecuser_feiyu_id')},
                        {field: 'is_huichuan', title: '回传',searchList: {"0":'不回传',"1":'回传'}, formatter: Table.api.formatter.status},
                        {field: 'huichuan_status', title: '回传结果',searchList: {"0":'未回传',"1":'回传成功',"2":'回传失败'}, formatter: Table.api.formatter.status},
                        {field: 'clue_convert_state', title: '线索状态',searchList: Config.clue_list, formatter: Table.api.formatter.status},
                        {field: 'crmId', title: __('Crmid'), operate: 'LIKE',searchable:false},
                        {field: 'wechat', title: '微信状态',operate: 'LIKE',searchable:false, visible:false,searchList: Config.wechat_list},
                        {field: 'yixiang', title: '意向度',operate: 'LIKE',searchable:false, visible:false,searchList: Config.yixiang_list},
                        {field: 'label_list_txt', title: __('Label'), operate: 'LIKE' ,searchable:false,},
                        {field: 'step', title: '客户进展', visible:false,searchList: Config.step_list},
                        {field: 'step_text', title: '客户进展', searchable:false},
//                        {field: 'md5', title: __('Md5'), operate: 'LIKE'},
                        {field: 'huichuantime', title: __('Huichuantime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
//                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, 
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: '回传',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-assign btn-dialog btn-ajax btn-restoreit',
                                    title: '我是标题', //按钮显示的文字，支持function
                                    url: '/api/feiyu.xiansuo/huichuan', //按钮的链接，支持使用{字段名}来占位替换，支持function
                                    confirm: '是否主动回传该客户？', //点击按钮后的确认框，支持function
                                    success: function (data,rest) {
                                        var opt = {url: ''};
                                        table.bootstrapTable('refresh', opt);
                                        return true;
                                    },
                                    visible: function (row) {
                                       if(row.is_huichuan == 1 && row.huichuan_status != 1){
                                           return true;
                                       }

                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
